package com.studentstreet;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.studentstreet.component.AbstractCommonActivity;
import com.studentstreet.tools.HtmlTools;
import com.studentstreet.tools.HttpHelper;
import com.studentstreet.tools.PreferenceTools;
import com.studentstreet.tools.Util;

/**
 * @author likebamboo
 * 
 */
public class OrderCountActivity extends AbstractCommonActivity {
	// Title标题
	private TextView titleText;
	// Login Button登录按钮
	private Button login_bt;
	// 头部左边的按钮
	private ImageButton backImgBt = null;

	private TextView countTv1 = null;
	private TextView countTv2 = null;
	private TextView countTv3 = null;
	private TextView countTv4 = null;

	private String userName = "";
	private String orderCountStr = "";

	// 信息下载进度框框
	private ProgressDialog progressDialog = null;
	// 去跟踪订单
	private LinearLayout goToTrackLayout = null;

	// 这里的值不能为AbstractCommonActivity中的0x0001,0x0002等
	private static final int REFRESH_MSG = 0x10001;
	private static final int EMPTY = 0x10002;
	private String EMPTY_COUNT = "-,-,-,-,-,-";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.order_count);
		// 初始化头部标题
		initHeader();
		// sharepreference
		PreferenceTools prefTools = new PreferenceTools(this);
		userName = prefTools.getUserName();
		// 登录后修改登录按钮上的text
		login_bt.setText(userName);
		// 去掉登录按钮文本右边的图标
		login_bt.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);

		countTv1 = (TextView) findViewById(R.id.order_count1);
		countTv2 = (TextView) findViewById(R.id.order_count2);
		countTv3 = (TextView) findViewById(R.id.order_count3);
		countTv4 = (TextView) findViewById(R.id.order_count4);

		goToTrackLayout = (LinearLayout) findViewById(R.id.order_count_go_track);
		goToTrackLayout.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(OrderCountActivity.this,
						OrderTrackActivity.class);
				startActivity(intent);
			}
		});

		// 显示登录进度对话框
		progressDialog = new ProgressDialog(this);
		progressDialog.setMessage(getResources().getString(
				R.string.order_count_loading));
		progressDialog.show();
		Thread orderCount = new Thread(new OrderCountThread());
		orderCount.start();
	}

	// 初始化头部信息
	private void initHeader() {
		// 返回按钮
		backImgBt = (ImageButton) findViewById(R.id.header_imgBt);
		// 头部中间的标题
		titleText = (TextView) findViewById(R.id.title);
		// 登录按钮
		login_bt = (Button) findViewById(R.id.login_bt);
		// 返回按钮
		backImgBt.setImageResource(R.drawable.back);
		backImgBt.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		// 头部中间的标题
		titleText.setText(getResources().getString(R.string.order_count));
	}

	class OrderCountThread implements Runnable {

		@Override
		public void run() {
			// 使用apache HTTP客户端实现
			String urlStr = Util.BASE_URL + "/servlet/OrderCountServlet";
			// 创建网络连接工具对象
			HttpHelper helper = new HttpHelper(urlStr);
			// 添加参数
			// 用户名
			helper.addParam("userName", userName);
			// 获得服务器返回的数据
			orderCountStr = helper.getResponse();
			if ("".equals(orderCountStr)) {
				// 当服务器返回的数据为空时
				sendMessage(EMPTY);
				return;
			}
			// 通知listView 有更新
			sendMessage(REFRESH_MSG);
		}
	}

	@Override
	protected void handleOtherMessage(int flag) {
		super.handleOtherMessage(flag);
		// 让progressDialog消失
		if (progressDialog.isShowing()) {
			progressDialog.cancel();
		}
		switch (flag) {
		case REFRESH_MSG:
			initCountMsg(orderCountStr.split(","));
			break;
		case EMPTY:
			initCountMsg(EMPTY_COUNT.split(","));
			showMsg();
			break;
		default:
			break;
		}
	}

	private void showMsg() {
		Toast.makeText(this,
				getResources().getString(R.string.cannot_arrived_server),
				Toast.LENGTH_SHORT).show();
	}

	private void initCountMsg(String[] orderCount) {
		countTv1.setText(HtmlTools.getInfo(this.getApplicationContext(),
				R.string.order_count1, orderCount[0]));
		countTv2.setText(HtmlTools.getInfo(this.getApplicationContext(),
				R.string.order_count2, orderCount[1]));
		countTv3.setText(HtmlTools.getInfo(this.getApplicationContext(),
				R.string.order_count3, orderCount[2], orderCount[3]));
		countTv4.setText(HtmlTools.getInfo(this.getApplicationContext(),
				R.string.order_count4, orderCount[4], orderCount[5]));
	}

}
